# scores/router.py
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from ..database import get_db
from . import schemas, crud

router = APIRouter(prefix="/scores", tags=["scores"])

@router.post("/", response_model=schemas.Score, summary="添加成绩", description="创建新的成绩记录")
def create_score(score: schemas.ScoreCreate, db: Session = Depends(get_db)):
    return crud.crud_score.create(db, score)

@router.get("/student/{student_id}", response_model=List[schemas.Score], summary="获取学生成绩", description="根据学生ID获取所有成绩记录")
def read_scores_by_student(student_id: str, db: Session = Depends(get_db)):
    return crud.crud_score.get_by_student(db, student_id)

@router.get("/", response_model=List[schemas.Score], summary="获取所有成绩", description="获取所有成绩记录，支持分页")
def read_scores(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    return crud.crud_score.get_all(db, skip=skip, limit=limit)

@router.put("/{student_id}/{exam_sequence}", response_model=schemas.Score, summary="更新成绩", description="根据学生ID和考试序次更新成绩记录")
def update_score(student_id: str, exam_sequence: int, score: schemas.ScoreUpdate, db: Session = Depends(get_db)):
    db_score = crud.crud_score.update(db, student_id, exam_sequence, score)
    if db_score is None:
        raise HTTPException(status_code=404, detail="Score not found")
    return db_score

@router.delete("/{student_id}/{exam_sequence}", summary="删除成绩", description="根据学生ID和考试序次删除成绩记录")
def delete_score(student_id: str, exam_sequence: int, db: Session = Depends(get_db)):
    success = crud.crud_score.delete(db, student_id, exam_sequence)
    if not success:
        raise HTTPException(status_code=404, detail="Score not found")
    return {"message": "Score deleted successfully"}